/**
 * 房間视图控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-7 12:55
 */
package com.addcn.texas.view
{

	import com.addcn.common.event.ParamEvent;
	import flash.display.*;
	import flash.events.Event;
	import flash.display.Stage;		
	import flash.events.MouseEvent;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;
	import com.addcn.texas.model.*;
	import com.addcn.texas.view.components.BetPanel;
	
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
		
	public class RoomView extends View
	{				
		protected var _container : Object;
		private var _app : AppLibary = AppLibary.getInstance();	
		private var _class : Class;
		private var _components : Object;
		
		public function RoomView(r_target : Object) {
			_container = r_target;	
			_intialize();
		}			
		
		/**
		 * 該視圖中介器需要偵聽的通知
		 */
		override public function notificationsList():void 
		{
			notifications = [
								RoomModel.ROOM_DATA_REDY,
								//RoomUserModel.SET_BET_LIMIT,								
								RoomUserModel.USER_SITDOWN,								
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {
				case RoomModel.ROOM_DATA_REDY: 			
					_setRoomName(r_data.name); 
					break;		
				//case RoomUserModel.SET_BET_LIMIT:
					//_showBetButton(r_data); //顯示下注按鈕
					//break;
				case RoomUserModel.USER_SITDOWN:					
					var _msg : String = "[系統]:" + r_data["name"] +" 在" + r_data["sit"]  +"號位置坐下。\n";
					_systemMsg(_msg);
					
					//for each(var val in r_data) {
						//_sit = val["sit"];
						//
						//
					//}
					break;					
				

			}			
		}
		
		private function _intialize() : void
		{						
			var _sprite = new Sprite;
			_container.addChild(_sprite);
			_container = _sprite;			
			
			_components = { };
			_class = this._app.getClass("GameArea");
			var _game_area = new _class;
			_game_area.cacheAsBitmap = true
			_container.addChild(_game_area);			
			_components["GameArea"] = _game_area;	
			//_components["GameArea"]["BetButton"].addEventListener(BetPanel.BET_EVENT, _betHandler);		
			//_components["GameArea"]["BetButton"].visible = false;
			_setTopBtn();
		}
		
		private function _setRoomName(r_name : String) : void {
			var _class = this._app.getClass("RoomLabel");
			if (_class == null) return;	
			var _room_label = new _class;
			_container.addChild(_room_label);
			_room_label.RoomName.text = r_name;
			_components["RoomLabel"] = _room_label;			
		}
		
		private function _setTopBtn() : void
		{
			var _class = this._app.getClass("RoomTopBtn");
			if (_class == null) return;	
			var _top_btn = new _class;
			_container.addChild(_top_btn);
			_top_btn.x = 578;
			_top_btn.y = 4;			
			_components["RoomTopBtn"] = _top_btn;	
			_top_btn.addEventListener(MouseEvent.CLICK, topBtnClickHandler);
		}
		
		
		private function  topBtnClickHandler(evt : MouseEvent) : void
		{
			switch(evt.target.name) {
				case "up":
					if(!Controller.getInstance().hasCommand(userOperationCommand)){
						Controller.getInstance().addCommand(userOperationCommand);
					}
					sendNotification(userOperationCommand,{type:"up"});
					break;
				case "back":
					if(!Controller.getInstance().hasCommand(BackToHallCommand)){
						Controller.getInstance().addCommand(BackToHallCommand);
					}
					sendNotification(BackToHallCommand);							
					break;
				case "help":
					break;
			}
		}
		
		private function _showBetButton(r_data : Object) : void
		{
			//_components["GameArea"]["BetButton"].visible = true;			
			//if(r_data["maxChip"]){
				//_components["GameArea"]["BetButton"].setLimit(r_data["maxChip"]);
			//}
			//if(r_data["limp"]){
				//_components["GameArea"]["BetButton"].setLimp(r_data["limp"]);
			//}
		}
		
		private function _betHandler(evt : ParamEvent) : void
		{
			var _data : Object = { };
			_data["type"] = "bet";
			_data["way"] = evt.params["way"];
			_data["chip"] = evt.params["chip"];								
			sendNotification(userOperationCommand, _data);			
			_components["GameArea"]["BetButton"].visible = false;
		}
					
		private function _systemMsg(r_msg : String) : void
		{
			this._components["GameArea"]["SystemMsg"].text += r_msg;
		}
		override public function destroy():void 
		{
			_components["RoomTopBtn"].removeEventListener(MouseEvent.CLICK, topBtnClickHandler);
			for each(var _val in _components) {
				if (_val != null && _val.parent) {
					_val.parent.removeChild(_val);
				}
			}
			RemoveAllChildren.remove(this._container as DisplayObject);			
		}
	}	
	
}
